<?php

/* ----------------------------------------------
  Caloris: Administration Pages

  $Id$
  $Author$

  Seznam stránek
  ---------------------------------------------- */

require_once("../../../index.php");
Caloris\ACL::authenticate();

define('_MODULE_ACTIVE', 'pageeditor');

if ($_POST["function_name"] == 'page_delete') {
        $item_id = $_POST["name"];
        $filename = _CALSET_PATHS_BASE . _CALSET_DIRS_PAGES . '/' . $item_id . '.html';

    // Smazání stránky z pages.xml
    $doc = new DOMDOcument;
    $doc->load(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

    $xpath = new DOMXpath($doc);
    $qry = '//page[@id="' . $item_id . '"]';

    foreach ($xpath->query($qry) as $node) {
        $node->parentNode->removeChild($node);
    }

    $doc->save(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');

    // Smazání stránky z adresáře cal-pages
    if (file_exists($filename)) {
        unlink($filename);
    }

    if ($_POST["j"] != 1) {
        header("location: " . $_SERVER["HTTP_REFERER"] . "");
        exit();
    } else {
        echo "{\"msg\": \"done\"}";
        exit();
    }
}

$content = '
<span style="color: red;">[cal:predefs(msg) /]</span>

<h1>Stránky</h1>

<form action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/pages.detail.php" method="post" class="form-inline">
<input type="hidden" name="function_name" value="page_new" /><input type="text" name="name" />&nbsp;<input type="submit" value="Vytvořit" class="btn btn-primary" />
</form>' . PHP_EOL;

// Listování stránek
$xmlPagesF = simplexml_load_file(_CALSET_PATHS_BASE . _CALSET_DIRS_DATA . '/pages.xml');
$xmlPages = $xmlPagesF->xpath('//page');

// sort by name
function sort_trees($t1, $t2) {
    return strcoll($t1["id"], $t2["id"]);
}

usort($xmlPages, 'sort_trees');

if (count($xmlPages) > 0) {
    $content .= '
<table class="table table-striped">
    <tr>
        <th style="width: 20px;">&nbsp;</th>
        <th style="width: 20px;">&nbsp;</th>
        <th>Název</th>
        <th>Zobrazení</th>
        <th>Titulek</th>
        <th>Modul</th>
    </tr>' . PHP_EOL;

    for ($a = 0; $a < count($xmlPages); $a++) {
        if ($xmlPages[$a]->system == 1) {
            $delete = '';
        } else {
            $delete = '
<form id="formshw_' . $a . '" action="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/index.php" method="post" name="trbl_' . $a . '" onsubmit="return false;">
<input type="hidden" name="function_name" value="page_delete" />
<input type="hidden" name="name" value="' . $xmlPages[$a]["id"] . '" />
<input type="submit" id="formx_' . $a . '" alt="x" value="Smazat" class="btn btn-danger" />
</form>' . PHP_EOL;
        }

        $content .= '
    <tr id="trbl_' . $a . '" >
        <td>' . $delete . '</td>
        <td><a href="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/pages.properties.php?name=' . $xmlPages[$a]["id"] . '" class="btn btn-primary">Vlastnosti</a></td>
        <td><a href="' . _CALSET_PATHS_URI . _CALSET_DIRS_MODULE . '/pageeditor/pages.detail.php?name=' . $xmlPages[$a]["id"] . '">' . $xmlPages[$a]["id"] . '</a></td>
        <td>' . $showPage . '</td>
        <td>' . Caloris\Text::caltag($xmlPages[$a]->title) . '</td>
        <td>' . Caloris\Text::caltag($xmlPages[$a]->module) . '</td>
    </tr>';
    }

    $content .= '
</table>' . PHP_EOL;

} else {
    $content .= '<p>Žádné stránky nenalezeny</p>';
}

$data["title"] = 'Stránky';

$tmpl = new Caloris\Template;
$tmpl->input(array($content));
$tmpl->renderHead($data);
echo $tmpl->renderBody('caloris-admin');